//
//  TableViewCell.m
//  UI_Project_A
//
//  Created by dllo on 16/1/25.
//  Copyright © 2016年 GH. All rights reserved.
//

#import "TableViewCell.h"
#import "ModelOfData.h"
#import <UIImageView+WebCache.h>
#import <DKNightVersion.h>

#define WIDTH self.frame.size.width
#define HEIGHT  self.frame.size.height
@interface TableViewCell ()
@property (nonatomic ,retain)UILabel *label;
@property (nonatomic ,retain)UIImageView *imageview;
@property (nonatomic, retain) ModelOfData *model;
@property (nonatomic ,retain)UILabel *labelTime;


@end

@implementation TableViewCell


- (void)dealloc {
    [_label release];
    [_imageview release];
    [_labelTime release];
    [super dealloc];

}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self) {
        [self createLabel];
        [self createImageview];
    }

    return self;
}


#pragma mark - 传值
- (void)passDataWithModel:(ModelOfData *)model {

    self.label.text = model.title;

   
    if (model.imageurls.count == 1) {
        NSDictionary *dicurl = model.imageurls[0];
    NSString *imageurl = [dicurl objectForKey:@"url"];
        
        NSString *url = [self Substring:imageurl];
        
        
        [self.imageview sd_setImageWithURL:[NSURL URLWithString:url]];
    }
    
    
    //timeLabel
    //时间
    
    NSString *modelTime = [model.ts substringToIndex:10];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm"];
    [dateFormatter setDateFormat:@"MM-dd"];
    [dateFormatter release];
    
    
    
    
    
    NSDate *now = [NSDate date];
    NSTimeInterval time = [now timeIntervalSince1970];
    float seconds = (int)time - modelTime.doubleValue;
    float points = seconds / 60;
    if (points < 60) {
        self.labelTime.text = [NSString stringWithFormat:@"%d分钟前", (int)points];
    }else if (points > 60 && points < 3600) {
        
        self.labelTime.text = [NSString stringWithFormat:@"1小时前"];
    }else {
        self.labelTime.text = [NSString stringWithFormat:@"2小时前"];
    }
    
    
    
    
    
    
    
    
}

- (NSString *)Substring:(NSString *)urlString {
    
    NSString *sub = urlString;
    
    if ([sub hasSuffix:@"jpg"]) {
        return urlString;
    }else {
        NSRange range = [urlString rangeOfString:@"u="];
        NSString *sub1 = [urlString substringFromIndex:range.location+2];
        
        NSRange lastRange = [sub1 rangeOfString:@"&"];
        sub = [sub1 substringToIndex:lastRange.location];
        
    }
    
    
    
    return sub;
}



#pragma mark - label
- (void)createLabel {

    self.label = [[UILabel alloc]init];
    [self addSubview:self.label];
    [self.label release];

    self.label.numberOfLines = 0;

   
    self.labelTime = [[UILabel alloc] init];
    [self addSubview:self.labelTime];
    [self.labelTime release];
    self.labelTime.numberOfLines = 0;
    
    self.label.dk_textColorPicker = DKColorWithColors([UIColor colorWithWhite:0.247 alpha:1.000],[UIColor whiteColor]);
    self.labelTime.dk_textColorPicker = DKColorWithColors([UIColor colorWithWhite:0.247 alpha:1.000],[UIColor whiteColor]);
    
}


- (void)createImageview {
   self.imageview = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@""]];
      [self addSubview:self.imageview];
    [self.imageview release];

}



- (void)layoutSubviews {
    [super layoutSubviews];
    
    
    if (!self.model.url) {
//       self.label.frame = CGRectMake(20, 10,WIDTH - 25, self.frame.size.height/2);
        self.label.frame = CGRectMake(20, 10, WIDTH/3 * 2 - 25, self.frame.size.height/2);
    } else {
        self.label.frame = CGRectMake(20, 10, WIDTH/3 * 2 - 25, self.frame.size.height/2);
    }

    
    self.labelTime.frame = CGRectMake(20, HEIGHT/5 * 3.5, 100, 30);

    
    
    
    self.imageview.frame = CGRectMake(WIDTH * 2/3, 10, WIDTH/3-10, HEIGHT - 10);

}




- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
